package com.lz.fruitday.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.exception.DbException;
import com.lz.fruitday.BaseApp;
import com.lz.fruitday.R;
import com.lz.fruitday.bean.Good;

import java.util.List;

/**
 * Created by liuZhi
 * 2015/10/28
 */
public class CarAdapter extends BaseAdapter implements View.OnClickListener{
    private List<Good> list;
    private LayoutInflater inflater;

    public CarAdapter(List<Good> list,Context context) {
        this.list = list;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list==null?null:list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            list=BaseApp.getInstance().getDbUtils().findAll(Good.class);
            Good good=list.get(position);
            ViewHolder vh;
            if (convertView==null){
                convertView=inflater.inflate(R.layout.item_cart_listview_layout,null);
                vh=new ViewHolder(convertView);
                vh.ivAdd.setOnClickListener(this);
                vh.ivDecrease.setOnClickListener(this);
                convertView.setTag(vh);
            }else {
                vh= (ViewHolder) convertView.getTag();
            }
            vh.ivAdd.setTag(position);
            vh.ivDecrease.setTag(position);
            vh.tvName.setText(good.getName());
            vh.tvPrice.setText(good.getPrice());
            vh.tvNum.setText(good.getNum());

        } catch (DbException e) {
            e.printStackTrace();
        }
        return convertView;
    }

    @Override
    public void onClick(View v) {
        int position = v.getTag() instanceof Integer ? (int) v.getTag() : 0;
        Good good=list.get(position);
        switch (v.getId()){
            case R.id.add_img:
                good.setNum(good.getNum() + 1);
                break;
            case R.id.decrease_img:
                if (good.getNum()>0){
                    good.setNum(good.getNum()-1);
                }
                break;
        }
        this.notifyDataSetChanged();
    }

    public class ViewHolder{
        private ImageView ivIcon;
        private TextView tvName;
        private TextView tvPrice;
        private TextView tvNum;
        private ImageView ivDecrease;
        private ImageView ivAdd;

        public ViewHolder(View convertView){
            ivIcon= (ImageView) convertView.findViewById(R.id.iv_icon);
            tvName= (TextView) convertView.findViewById(R.id.tv_product_name);
            tvPrice= (TextView) convertView.findViewById(R.id.tv_price);
            tvNum= (TextView) convertView.findViewById(R.id.tv_num);
            ivDecrease= (ImageView) convertView.findViewById(R.id.decrease_img);
            ivAdd= (ImageView) convertView.findViewById(R.id.add_img);

        }
    }
}
